package com.example.backend.controller;

import com.example.backend.entity.Driver;
import com.example.backend.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/driver")
public class DriverController {
    @Autowired
    private DriverService driverService;
    @PostMapping("/save")
    public Driver saveDriver(@RequestBody Driver driver){
        return driverService.save(driver);
    }
    @GetMapping("/list")
    public List<Driver> findAll(){
        return driverService.findAll();
    }
    @GetMapping("/search")
    public List<Driver> findDriverByName(@Param("key") String key,@Param("value") String value){
        switch (key){
            case "id":return driverService.findById(Integer.parseInt(value));
            case "name":return driverService.findByName(value);
            case "identity":return driverService.findByIdentity(value);
            case "age":return driverService.findByAge(Integer.parseInt(value));
            case "address":return driverService.findByAddress(value);
            case "phone":return driverService.findByPhone(value);
            case "license":return driverService.findByLicense(value);
            case "state":return driverService.findByState(value);
        }
        return null;
    }
    @GetMapping("/repeat")
    public boolean findName(@Param("name")String name){
        return driverService.findName(name);
    }
    @GetMapping("/changeRepeat")
    public boolean findNameAndId(@Param("id") Integer id,@Param("name")String name){
        System.out.println(name);
        return driverService.findNameAndId(id,name);
    }
    @PostMapping("/update")
    public Driver updateDriver(@RequestBody Driver driver){
        return driverService.update(driver);
    }
    @GetMapping("/delete")
    public void deleteDriver(@Param("id")Integer id){
        driverService.delete(id);
    }
}
